home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10852 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.6 KB  |  165 lines

  1. Path: ix.netcom.com!news
  2. From: jshoor@ix.netcom.com(Jonas Shoor )
  3. Newsgroups: comp.lang.c,comp.unix.programmer
  4. Subject: Problems execing passwd using pipes
  5. Date: 20 Mar 1996 14:00:49 GMT
  6. Organization: Netcom
  7. Message-ID: <4ip32h$srk@dfw-ixnews3.ix.netcom.com>
  8. NNTP-Posting-Host: ix-dc10-19.ix.netcom.com
  9. X-NETCOM-Date: Wed Mar 20  8:00:49 AM CST 1996
  10.  
  11. I am trying to write a program to execute the passwd command.
  12.  
  13. Passwd is interactive, so I set two pipes (one to read from the process
  14. and one to write to the process), and I fork off a process to execute
  15. the command. 
  16.  
  17. With this program, I can execute passwd, read its response (New
  18. passwd:), send the password string, but when I try to read the second
  19. response, it hangs. But if I hit the enter key the program will return
  20. "\nReenter new password:". I have tried to add "\n"s to the end
  21. of the passwd string, but it doesnt help.
  22.  
  23. The code follows, it is modeled after examples in the "Using C on the
  24. UNIX System" O'Reilly book.
  25.  
  26. Any ideas would be greatly appreciated, as I have exhausted all of
  27. mine.
  28.  
  29. Thanks,
  30. Jonas
  31.  
  32. #include<stdio.h>
  33.  
  34. main()
  35. {
  36. FILE *fp1, *fp2;
  37. int pid, pipefds1[2], pipefds2[2];
  38. char username[14], username2[24];
  39.  
  40. if (pipe(pipefds1) < 0){
  41.    perror("pipe1");
  42.    exit(1);
  43. }
  44. if (pipe(pipefds2) < 0){
  45.    perror("pipe2");
  46.    exit(1);
  47. }
  48.  
  49. if ((pid=fork()) < 0){
  50.    perror("fork");
  51.    exit(1);
  52. }
  53. if (pid == 0){
  54.    close(2);        /* close stderr */
  55.    dup(pipefds1[1]);    /* set stderr to pipefds1[1] */
  56.    close(pipefds1[1]);
  57.    close(pipefds1[0]);    /*close read side of pipefds1 */
  58.    close(0);        /* close stdin */
  59.    dup(pipefds2[0]);    /* set stdin to pipefds2[0] */
  60.    close(pipefds2[0]);
  61.    close(pipefds2[1]);  /* close write side of pipefds2 */
  62.    execl("/usr/bin/passwd", "passwd", "testvue", (char *) 0);
  63.    perror("exec");
  64.    exit(1);
  65. }
  66. close(pipefds1[1]);     /* close write side of pipefds1 */
  67. close(pipefds2[0]);    /* close read side of pipefds2 */
  68. fp1=fdopen(pipefds1[0],"r");
  69. fread(username, sizeof(char), 13, fp1); /* Read- New password: */
  70. username[13]='\0';
  71. printf("First Prompt = %s \n",username); /* Verify prompt */
  72.  
  73. fp2=fdopen(pipefds2[1],"w"); /* Open pipefds2[1] for writing */
  74. fprintf(fp2, "aaaaa"); 
  75. fclose(fp2);
  76. printf("wFrom: jshoor@ix.netcom.com(Jonas Shoor )
  77. Newsgroups: comp.lang.c,comp.unix.programmer
  78. Subject: Problems execing passwd using pipes
  79.  
  80. I am trying to write a program to execute the passwd command.
  81.  
  82. Passwd is interactive, so I set two pipes (one to read from the process
  83. and one to write to the process), and I fork off a process to execute
  84. the command. 
  85.  
  86. With this program, I can execute passwd, read its response (New
  87. passwd:), send the password string, but when I try to read the second
  88. response, it hangs. But if I hit the enter key the program will return
  89. "\nReenter new password:". I have tried to add "\n"s to the end
  90. of the passwd string, but it doesnt help.
  91.  
  92. The code follows, it is modeled after examples in the "Using C on the
  93. UNIX System" O'Reilly book.
  94.  
  95. Any ideas would be greatly appreciated, as I have exhausted all of
  96. mine.
  97.  
  98. Thanks,
  99. Jonas
  100.  
  101. #include<stdio.h>
  102.  
  103. main()
  104. {
  105. FILE *fp1, *fp2;
  106. int pid, pipefds1[2], pipefds2[2];
  107. char username[14], username2[24];
  108.  
  109. if (pipe(pipefds1) < 0){
  110.    perror("pipe1");
  111.    exit(1);
  112. }
  113. if (pipe(pipefds2) < 0){
  114.    perror("pipe2");
  115.    exit(1);
  116. }
  117.  
  118. if ((pid=fork()) < 0){
  119.    perror("fork");
  120.    exit(1);
  121. }
  122. if (pid == 0){
  123.    close(2);        /* close stderr */
  124.    dup(pipefds1[1]);    /* set stderr to pipefds1[1] */
  125.    close(pipefds1[1]);
  126.    close(pipefds1[0]);    /*close read side of pipefds1 */
  127.    close(0);        /* close stdin */
  128.    dup(pipefds2[0]);    /* set stdin to pipefds2[0] */
  129.    close(pipefds2[0]);
  130.    close(pipefds2[1]);  /* close write side of pipefds2 */
  131.    execl("/usr/bin/passwd", "passwd", "testvue", (char *) 0);
  132.    perror("exec");
  133.    exit(1);
  134. }
  135. close(pipefds1[1]);     /* close write side of pipefds1 */
  136. close(pipefds2[0]);    /* close read side of pipefds2 */
  137. fp1=fdopen(pipefds1[0],"r");
  138. fread(username, sizeof(char), 13, fp1); /* Read- New password: */
  139. username[13]='\0';
  140. printf("First Prompt = %s \n",username); /* Verify prompt */
  141.  
  142. fp2=fdopen(pipefds2[1],"w"); /* Open pipefds2[1] for writing */
  143. fprintf(fp2, "aaaaa"); 
  144. fclose(fp2);
  145. printf("writing to pipe2\n"); /* verify write */
  146.  
  147. fread(username2, sizeof(char), 23, fp1); /* Read- Re-enter new
  148. password: */
  149. read(pipefds1[0],username2,23);         /* It hangs HERE!! */
  150. fclose(fp1); 
  151. username[23]='\0';
  152. printf("Second Prompt = %s \n",username2); /* Verify prompt */
  153. printf("End\n");
  154.  
  155.  
  156. /* Send answer matching answer to second prompt
  157.    then wait for child process to finish */
  158.  
  159. exit(0);
  160. }
  161.  
  162.  
  163. PS I am root when I execute this, and testvue does exist. Also, I am
  164. running on a sparc5 with Solaris 2.3
  165.